package com.example.newsapp.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.newsapp.model.entity.VideoData;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.example.newsapp.network.RetrofitClient;
import com.example.newsapp.network.VideoApiService;

public class VideoViewModel extends ViewModel {
    private final MutableLiveData<VideoData> videoData = new MutableLiveData<>();
    private final MutableLiveData<String> errorMessage = new MutableLiveData<>();
    private final VideoApiService apiService;

    public VideoViewModel() {
        apiService = RetrofitClient.getInstance().getApiService();
    }

    public LiveData<VideoData> getVideoData() {
        return videoData;
    }

    public LiveData<String> getErrorMessage() {
        return errorMessage;
    }

    public void getVideoList(int page) {
        apiService.getVideoList(page).enqueue(new Callback<VideoData>() {
            @Override
            public void onResponse(Call<VideoData> call, Response<VideoData> response) {
                if (response.isSuccessful() && response.body() != null) {
                    videoData.postValue(response.body());
                } else {
                    errorMessage.postValue("获取视频数据失败");
                }
            }

            @Override
            public void onFailure(Call<VideoData> call, Throwable t) {
                errorMessage.postValue("网络请求失败: " + t.getMessage());
            }
        });
    }
}